//
//  ClassifyViewController.swift
//  Eternity
//
//  Created by cll12_12 on 2016/12/5.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit

class ClassifyViewController: BaseViewController {

    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        initBaseLayout()
        initNavItem(controller: self, imageName: "nav_search_n", highlitedImageName: "nav_search_h", selectedImageName: "", isLeft: false)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    // MARK: - private method
    func initBaseLayout() {
        self.view.addSubview(collection)
    }
    
    func layoutPageSubviews() {
        
    }
    
    // MARK: - setters and getters
    var _layout: UICollectionViewFlowLayout!
    var layout: UICollectionViewFlowLayout {
        if _layout == nil {
            _layout = UICollectionViewFlowLayout()
            _layout.minimumLineSpacing = 0
            _layout.minimumInteritemSpacing = 0
        }
        return _layout
    }
    
    var _collection: UICollectionView!
    var collection: UICollectionView {
         if _collection == nil{
            _collection = UICollectionView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT), collectionViewLayout: layout)
            _collection.backgroundColor = UIColor(hexString: "#E5E5E5")
            _collection.delegate = self
            _collection.dataSource = self
            _collection.showsVerticalScrollIndicator = false
            _collection.register(ClassifyCell.self, forCellWithReuseIdentifier: "ClassifyCell")
            _collection.register(ClassifyHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "ClassifyHeader")
        }
        return _collection
    }
}

extension ClassifyViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 4
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 8
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let string = "ClassifyCell"
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: string, for: indexPath) as! ClassifyCell
        
        cell.configUI()
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: SCREEN_WIDTH/4, height: 100)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: SCREEN_WIDTH, height: 50)
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let string = "ClassifyHeader"
        
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: string, for: indexPath) as! ClassifyHeader
        header.configUI()
        
        return header
    }
    
}
